//package vip.xiaonuo.modular.reactor;
//
//import static reactor.event.selector.Selectors.$;
//
//import cn.hutool.core.util.ObjectUtil;
//import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
//import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
//import java.util.Date;
//import java.util.List;
//import java.util.stream.Collectors;
//import javax.annotation.Resource;
//import org.apache.commons.collections4.CollectionUtils;
//import org.apache.commons.lang3.StringUtils;
//import org.springframework.beans.factory.InitializingBean;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.stereotype.Service;
//import reactor.core.Reactor;
//import reactor.event.Event;
//import reactor.function.Consumer;
//import vip.xiaonuo.facedoor.modular.memberuser.entity.MemberUser;
//import vip.xiaonuo.facedoor.modular.memberuser.service.MemberUserService;
//import vip.xiaonuo.facedoor.modular.reactor.config.EventTopic;
//import vip.xiaonuo.facedoor.modular.visitor.entity.Visitor;
//import vip.xiaonuo.facedoor.modular.visitor.service.VisitorService;
//import vip.xiaonuo.modular.remoteapi.stairs.RemoteStairsConstant;
//import vip.xiaonuo.modular.remoteapi.stairs.RemoteStairsConstant.DeviceSyncStatus;
//import vip.xiaonuo.modular.remoteapi.stairs.RemoteStairsConstant.RequestCmd;
//import vip.xiaonuo.modular.sync.DeviceEvent;
//import vip.xiaonuo.modular.ybuildingmemberrl.entity.YBuildingMemberRl;
//import vip.xiaonuo.modular.ybuildingmemberrl.mapper.YBuildingMemberRlMapper;
//import vip.xiaonuo.modular.ybuildingmemberrl.param.YBuildingMemberRlParam;
//import vip.xiaonuo.modular.ybuildingvisitorrl.entity.YBuildingVisitorRl;
//import vip.xiaonuo.modular.ybuildingvisitorrl.mapper.YBuildingVisitorRlMapper;
//import vip.xiaonuo.modular.ybuildingvisitorrl.param.YBuildingVisitorRlParam;
//import vip.xiaonuo.modular.ydeviceevent.entity.YDeviceEvent;
//import vip.xiaonuo.modular.ydeviceevent.mapper.YDeviceEventMapper;
//import vip.xiaonuo.modular.yelevatordevice.entity.YElevatorDevice;
//import vip.xiaonuo.modular.yelevatordevice.service.YElevatorDeviceService;
//
//@Service
//public class StarisVisitorUpdateCardListener implements Consumer<Event<Long>>, InitializingBean {
//
//    @Resource
//    private YBuildingVisitorRlMapper buildingVisitorRlMapper;
//
//    @Autowired
//    private YElevatorDeviceService elevatorDeviceService;
//
//    @Resource
//    private YDeviceEventMapper deviceEventEntityMapper;
//
//    @Autowired
//    private Reactor reactor;
//
//    @Autowired
//    private VisitorService visitorService;
//
//    @Override
//    public void accept(Event<Long> event) {
//        List<YBuildingVisitorRl> list = buildingVisitorRlMapper.selectList(
//                new LambdaQueryWrapper<YBuildingVisitorRl>().eq(YBuildingVisitorRl::getVisitorId, event.getData()));
//        Visitor visitor = visitorService.getById(event.getData());
//
//        if (CollectionUtils.isNotEmpty(list)) {
//            // 卡号如果为null了删除梯控内容
//            if (StringUtils.isBlank(visitor.getIcCard())) {
//
//                // 如果是小程序 直接删除、因为没有下发到电梯里
//                if (ObjectUtil.equal(visitor.getSource(),2)){
//                    buildingVisitorRlMapper.deleteBatchIds(list.stream().map(YBuildingVisitorRl::getId).collect(Collectors.toList()));
//                    return;
//                }
//                for (YBuildingVisitorRl buildingVisitorRl : list) {
//                    // 新增中 直接删除
//                    if (ObjectUtil.equal(buildingVisitorRl.getDeviceSyncStatus(), DeviceSyncStatus.ADD)) {
//                        buildingVisitorRlMapper.deleteById(buildingVisitorRl.getId());
//                        continue;
//                    }
//                    // 其他的删除梯控任务
//                    buildingVisitorRl.setDeviceSyncStatus(DeviceSyncStatus.DELETE);
//                    buildingVisitorRlMapper.updateById(buildingVisitorRl);
//                    // 存在任务表
//                    YElevatorDevice device = elevatorDeviceService.getById(buildingVisitorRl.getDeviceId());
//                    YDeviceEvent deviceEvent = new YDeviceEvent();
//                    deviceEvent.setDeviceNo(device.getDeviceNo());
//                    deviceEvent.setType(RemoteStairsConstant.RequestCmd.DELETE_PERSON);
//                    deviceEvent.setCreateDate(new Date());
//                    deviceEvent.setBody(buildingVisitorRl.getVisitorId() + ":" + buildingVisitorRl.getVisitorId());
//                    deviceEvent.setStatus(DeviceEvent.NEW);
//                    deviceEvent.setSysType(1);
//                    deviceEventEntityMapper.insert(deviceEvent);
//                }
//            } else {
//                // 卡号不为null 那么直接操作同步完毕的记录，让其去做修改
//
//                // 如果是小程序的话  不做修改因为和卡号没关系
//                if (ObjectUtil.equal(visitor.getSource(),2)){
//                    return;
//                }
//                for (YBuildingVisitorRl yBuildingVisitorRl : list) {
//                    if (ObjectUtil.equal(yBuildingVisitorRl.getDeviceSyncStatus(), DeviceSyncStatus.SYNC_SUCCESS)) {
//                        // 修改卡号
//                        yBuildingVisitorRl.setDeviceSyncStatus(RemoteStairsConstant.DeviceSyncStatus.UPDATE);
//                        buildingVisitorRlMapper.updateById(yBuildingVisitorRl);
//                        // 插入人员修改任务表
//                        YElevatorDevice device = elevatorDeviceService.getById(yBuildingVisitorRl.getDeviceId());
//                        YDeviceEvent deviceEvent = new YDeviceEvent();
//                        deviceEvent.setDeviceNo(device.getDeviceNo());
//                        deviceEvent.setStatus(DeviceEvent.NEW);
//                        deviceEvent.setCreateDate(new Date());
//                        deviceEvent.setSysType(2);
//                        deviceEvent.setBody(yBuildingVisitorRl.getId().toString());
//                        deviceEvent.setType(RemoteStairsConstant.RequestCmd.UPDATE_PERSON);
//                        deviceEventEntityMapper.insert(deviceEvent);
//                    }
//                }
//            }
//        }
//    }
//
//    @Override
//    public void afterPropertiesSet() throws Exception {
//        reactor.on($(EventTopic.VISITOR_UPDATE_CARD), this);
//    }
//}
